package com.food.platformmanagement.domain;

import javax.persistence.Column;
import javax.persistence.Embeddable;

/**
 * 用户名的值对象
 * 用户名应为数字+字符组成的字符串
 * 长度不超过16
 * @author 24270
 */
@Embeddable
public class UserName {

    @Column(name = "name")
    private String name;

    /**
     * TODO 这个方法怎么调用还没学到，先这样
     * @param name
     */
    public UserName(String name) {
        name(name);
    }

    public UserName() {

    }

    public void name(String name) {
        if(name == null || name.isEmpty()) {
            throw new UserNameException("用户名不能为空");
        }
        if(!name.matches("[0-9|a-z|A-Z]+")) {
            throw new UserNameException("用户名必须由数字或字母组成");
        }
        if(name.length() > 16) {
            throw new UserNameException("用户名长度不能超过16");
        }
        this.name = name;
    }

    public String value() {
        return name;
    }

}
